cc.Class({
    extends: cc.Component,

    properties: {
        //页面容器
        content: cc.Node,
        
        pageDemo : cc.Prefab,
    },
    onDestroy: function () {
        cc.utils.msgHub.delListener(this);
        // cc.view.setResizeCallback( null );
    },
    onLoad: function () {
        // cc.view.setResizeCallback( this.onResize.bind(this) );
        cc.utils.msgHub.addListener(cc.utils.macro.EVENTS.PUSH_PAGE_FADE, this.resPushPageFade, this);
        cc.utils.msgHub.addListener(cc.utils.macro.EVENTS.PUSH_PAGE, this.resPushPage, this);
        if( typeof IS_WEIXIN != "undefined" && IS_WEIXIN ) {
            if( !myWx.ready ) {
                cc.utils.msgHub.addListener(cc.utils.macro.EVENTS.WX_READY, this.startGame, this);
            }
        }

        this.content.destroyAllChildren();
        this.curPage = null;

        //collider
        // this.manager = cc.director.getCollisionManager();
        // this.manager.enabled = true;
        // this.manager.enabledDebugDraw = true;

        //删除DEBUG的窗口。
        try {
            var fps = document.getElementById("fps");
            document.body.removeChild(fps);
        } catch (e) { }

    },
    // onResize(){
    //     domLayerScreenTip.resize();
    // },
    start: function () {
        if( CC_DEBUG ) {
            this.startGame();
        }
        else {
            
            if (typeof IS_WEIXIN != "undefined" && !IS_WEIXIN) {
                if (!cc.utils.soundMgr.bgmList.bgm) {
                    cc.utils.soundMgr.playBgm("bgm");
                }
            }
    
            try {
                domHelper.init(this);
            } catch (e) { }
    
            if( typeof IS_WEIXIN != "undefined" && IS_WEIXIN ) {
                if( myWx.ready ) {
                    this.startGame();
                }
            }
            else {
                this.startGame();
            }
        }
    },
    startGame: function () {
        if (this.gameStarted) {
            return;
        }

        this.gameStarted = true;

        cc.utils.soundMgr.playBgm("bgm");

        if( CC_DEBUG ) {
            this.resPushPage("pageDemo");
        }
        else {
            this.resPushPage( "pageDemo" );
        }
        // this.resPushPage( "pageTest" );
    },
    resPushPage: function (name) {
        if (!this[name]) {
            cc.log("没找到页面");
            return;
        }
        var prefab = this[name];
        var panel = cc.instantiate(prefab);
        this.content.addChild(panel);
        this.curPage = panel;
    },
    resPushPageFade: function (name) {
        if (!this[name]) {
            cc.log("没找到页面");
            return;
        }
        if (cc.isValid(this.curPage)) {
            this.curPage.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function () {
                this.destroy();
            }.bind(this.curPage))));
        }
        this.resPushPage( name );
        this.curPage.opacity = 0;
        this.curPage.runAction(cc.fadeIn(0.8));
    },
    onClickSound: function () {
        cc.utils.soundMgr.toggleMute();
    }
});
